
package desmontec.alertas

import desmontec.sistema.SecUser
import desmontec.alertas.TipoAlerta;
import desmontec.sistema.SecUser
import desmontec.util.Constantes

class AlertaDPGController {


	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	static final String FLAG_ALERTAR_TRUE = "true"

	AlertaDeletadoService alertaDeletadoService
	AlertaDPGService alertaDPGService
	
	def springSecurityService
	
	def index = {
		redirect(action: "listarAlertasPresentes", params: params)
	}
	//fernando.alves - inicio
	def listarAlertasPresentes = {
		//		def alertasDataInvalida =  AlertaDPG.findAllByFlagTipo(TipoAlerta.ALERTA_DATA_INVALIDA)
		def alertasDataInvalida =  alertaDPGService.alertasDoUsuario(TipoAlerta.ALERTA_DATA_INVALIDA)
		[alertaDPGInstanceList: alertasDataInvalida, alertaDPGInstanceTotal: alertasDataInvalida.size()]
	}

	def listarAlertasFuturos = {
		//		def alertasVencimentoProximo =  AlertaDPG.findAllByFlagTipo(TipoAlerta.ALERTA_VENCIMENTO_PROXIMO)
		def alertasVencimentoProximo =  alertaDPGService.alertasDoUsuario(TipoAlerta.ALERTA_VENCIMENTO_PROXIMO)
		[alertaDPGInstanceList: alertasVencimentoProximo, alertaDPGInstanceTotal: alertasVencimentoProximo.size()]
	}

	def exibirAlertasPresentes = {
		//		def alertasDataInvalida =  AlertaDPG.findAllByFlagTipoAndFlagAlertar(TipoAlerta.ALERTA_DATA_INVALIDA, ALERTAR)
		def alertasDataInvalida =  alertaDPGService.alertasAtivosDoUsuario(TipoAlerta.ALERTA_DATA_INVALIDA)
		render (view:"exibirAlertas",
				model: [alertaDPGInstanceList: alertasDataInvalida, alertaDPGInstanceTotal: alertasDataInvalida.size()])
	}

	def exibirAlertasFuturos = {
		def alertasVencimentoProximo =  alertaDPGService.alertasAtivosDoUsuario(TipoAlerta.ALERTA_VENCIMENTO_PROXIMO)
		render (view:"exibirAlertas",
				model: [alertaDPGInstanceList: alertasVencimentoProximo, alertaDPGInstanceTotal: alertasVencimentoProximo.size()])
	}

	//fernando.alves - fim

	def create = {
		def alertaDPGInstance = new AlertaDPG()
		alertaDPGInstance.properties = params
		return [alertaDPGInstance: alertaDPGInstance]
	}

	def save = {
		def alertaDPGInstance = new AlertaDPG(params)
		if (alertaDPGInstance.save(flush: true)) {
			flash.message = "${message(code: 'desmontec.alertas.AlertaDPG.default.created.message', args: [message(code: 'alertaDPG.label', default: 'AlertaDPG'), alertaDPGInstance.id])}"
			redirect(action: "show", id: alertaDPGInstance.id)
		}
		else {
			render(view: "create", model: [alertaDPGInstance: alertaDPGInstance])
		}
	}

	def show = {
		def alertaDPGInstance = AlertaDPG.get(params.id)
		if (!alertaDPGInstance) {
			flash.message = "${message(code: 'desmontec.alertas.AlertaDPG.default.not.found.message', args: [message(code: 'alertaDPG.label', default: 'AlertaDPG'), params.id])}"
			redirect(action: "listarAlertasPresentes")
		}
		else {
			[alertaDPGInstance: alertaDPGInstance]
		}
	}

	def edit = {
		def alertaDPGInstance = AlertaDPG.get(params.id)
		if (!alertaDPGInstance) {
			flash.message = "${message(code: 'desmontec.alertas.AlertaDPG.default.not.found.message', args: [message(code: 'alertaDPG.label', default: 'AlertaDPG'), params.id])}"
			redirect(action: "listarAlertasPresentes")
		}
		else {
			return [alertaDPGInstance: alertaDPGInstance]
		}
	}

	def update = {
		def alertaDPGInstance = AlertaDPG.get(params.id)
		if (alertaDPGInstance) {
			if (params.version) {
				def version = params.version.toLong()
				if (alertaDPGInstance.version > version) {

					alertaDPGInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [
						message(code: 'alertaDPG.label', default: 'AlertaDPG')]
					as Object[], "Another user has updated this AlertaDPG while you were editing")
					render(view: "edit", model: [alertaDPGInstance: alertaDPGInstance])
					return
				}
			}
			alertaDPGInstance.properties = params
			if (!alertaDPGInstance.hasErrors() && alertaDPGInstance.save(flush: true)) {
				flash.message = "${message(code: 'desmontec.alertas.AlertaDPG.default.updated.message', args: [message(code: 'alertaDPG.label', default: 'AlertaDPG'), alertaDPGInstance.id])}"
				redirect(action: "show", id: alertaDPGInstance.id)
			}
			else {
				render(view: "edit", model: [alertaDPGInstance: alertaDPGInstance])
			}
		}
		else {
			flash.message = "${message(code: 'desmontec.alertas.AlertaDPG.default.not.found.message', args: [message(code: 'alertaDPG.label', default: 'AlertaDPG'), params.id])}"
			redirect(action: "listarAlertasPresentes")
		}
	}

	def delete = {
		def alertaDPGInstance = AlertaDPG.get(params.id)
		if (alertaDPGInstance) {
			try {
				//fernando.alves - inicio
				flash.message = "${message(code: 'desmontec.alertas.AlertaDPG.default.deleted.message', args: [message(code: 'alertaDPG.label', default: 'AlertaDPG'), params.id])}"
				//adiciona alertaDeletado
				alertaDeletadoService.adicionaAlertaDeletado(alertaDPGInstance.dpg.numero, alertaDPGInstance.id)
				alertaDPGInstance.delete(flush:true)
				//como jah esta na visao listarAlertasPresentes
				//devemos renderizar
				render(view: "listarAlertasPresentes", model: [alertaDPGInstance: alertaDPGInstance])
				//fernando.alves - fim
				redirect(action: "listarAlertasPresentes")
			}
			catch (org.springframework.dao.DataIntegrityViolationException e) {
				flash.message = "${message(code: 'desmontec.alertas.AlertaDPG.default.not.deleted.message', args: [message(code: 'alertaDPG.label', default: 'AlertaDPG'), params.id])}"
				redirect(action: "show", id: params.id)
			}
		}
		else {
			flash.message = "${message(code: 'desmontec.alertas.AlertaDPG.default.not.found.message', args: [message(code: 'alertaDPG.label', default: 'AlertaDPG'), params.id])}"
			redirect(action: "listarAlertasPresentes")
		}
	}

	//fernando.alves - inicio

	def atualizarFlagAlerta = {
		//atualiza flag de determinada instancia de AlertaDPG
		//de acordo com o valor do checkbox associada a mesma
		def alerta = AlertaDPG.get(params.id)
		def user = (SecUser) springSecurityService.getCurrentUser()
		if (params.alertar == FLAG_ALERTAR_TRUE){
			alerta.flagAlertar = Constantes.ALERTAR
		} else {
			alerta.flagAlertar = Constantes.NAO_ALERTAR
		}
		alerta.save(flush: true)
	}

	def alertarTodos = {

		//atualiza a para ALERTAR a flag de todas as instancias de determinado
		//tipo de alerta
		def user = (SecUser) springSecurityService.getCurrentUser()
		def tipoAlerta = getTipoEnum(params.tipoDoAlerta)
		if (tipoAlerta != null){
			for (AlertaDPG a in alertaDPGService.alertasDoUsuario(tipoAlerta)){
				a.flagAlertar = Constantes.ALERTAR
				a.save(flush:true)
			}
		}

		return true
	}

	def alertarNenhum = {

		//atualiza a para NAO ALERTAR a flag de todas as instancias de determinado
		//tipo de alerta
		def user = (SecUser) springSecurityService.getCurrentUser()
		def tipoAlerta = getTipoEnum(params.tipoDoAlerta)
		if (tipoAlerta != null){
			for (AlertaDPG a in alertaDPGService.alertasDoUsuario(tipoAlerta)){
				a.flagAlertar = Constantes.NAO_ALERTAR
				a.save(flush:true)
			}
		}
		
		return true
	}

	def getTipoEnum(String tipo) {
		if (tipo == "Data+invalidada"){
			return TipoAlerta.ALERTA_DATA_INVALIDA
		} else if (tipo == "Data+vencimento+proxima"){
			return TipoAlerta.ALERTA_VENCIMENTO_PROXIMO
		} else {
			return null
		}
	}
	//fernando.avles - fim

}